using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

/// <summary>
/// Summary description for Navigator
/// </summary>
public class Navigator
{
	public Navigator()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    /// <summary>
    /// Navigates to the default URL if session is not valid
    /// </summary>
    /// <param name="navigateURL">the URL to naivage to is invalid session. if null, goes to login page</param>
    //public static void validLogin(string navigateURL)
    //{
    //    if (HttpContext.Current.Session["userid"] == null)
    //    {
    //        if (navigateURL.Trim()=="")
    //            HttpContext.Current.Response.Redirect("default.aspx");
    //        else
    //            HttpContext.Current.Response.Redirect(navigateURL);
    //    }
    //}
    public string getUserHomePage()//implemenetd by sanal on April 03, 2008
    {
        try
        {
            if (HttpContext.Current.Session["userid"] == null) return "welcome.aspx?id=default";
            if (HttpContext.Current.Session["userid"].ToString().Trim() == "") return "welcome.aspx?id=default";
            HDDatabase hdd = new HDDatabase(); string mnuId = ""; string pgs_id = ""; DataRow drMnu=null;// = new DataRow();
            string homePageMenu = hdd.GetColumnValue("sy_usr", "home_page_menu", "user_id='" + HttpContext.Current.Session["userid"].ToString().Replace("'","''") + "'");
            if (homePageMenu != "")
            {
                DataTable dtMnu = new DataTable();
                dtMnu = hdd.GetDataTable("select * from sy_mnu where af_row_id='" + homePageMenu + "'");
                if (dtMnu != null)
                {
                    if (dtMnu.Rows.Count > 0)
                    {
                        mnuId = dtMnu.Rows[0]["af_row_id"].ToString();
                        pgs_id = dtMnu.Rows[0]["pageset_id"].ToString();
                        drMnu = dtMnu.Rows[0];
                    }
                }
                dtMnu = null;
            }
            hdd = null;
            if (homePageMenu == "") return "welcome.aspx?id=default";
            else
                return getMenuURL(drMnu);
                //return "DisplayGrid.aspx" + (mnuId.Trim().Length > 0 ? "?mn=" + mnuId : "") + (pgs_id.Trim().Length > 0 ? "&pgs_id=" + pgs_id : "");
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error " + e.Message + " in getUserHomePage Function");
            return "welcome.aspx?id=default";
        }
    }
    private string getMenuURL(DataRow drMenu)//implemenetd by sanal on April 03, 2008
    {
        if (drMenu == null) return "welcome.aspx?id=default";
        security sc = new security();
        string ret = checkPageset(sc.getMenuNavigateUrl(drMenu));
        sc = null;
        return ret.Trim() == "" ? "welcome.aspx?id=default" : ret;
    }
    private string checkPageset(string url)//implemenetd by sanal on April 05, 2008
    {
        if (url == "") return "";
        url = url.ToLower();//makes it to lower chrs.
        if (url.IndexOf(".aspx?")>-1)//if a vaild aspx file
        {
            string[] arr = url.Split('&');//split it through query string arguments
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].IndexOf("pgs_id=") > -1)//if a pagest is specified
                {
                    if (arr[i].Length < 8)//if pagest value does not exists
                        return "welcome.aspx?id=default";
                    else
                        break;
                }
            }
        }
        return url;
    }

    public static void storeURL(string url, bool ispostback, bool mustAdd,bool clearCache)//implemenetd by sanal on April 03, 2008
    {
        //string urll = url;
        ArrayList alURLs = null;
        if (HttpContext.Current.Session["URLs"] == null)//if null creates the array
            alURLs = new ArrayList();
        else
            alURLs = (ArrayList)HttpContext.Current.Session["URLs"];//uses the existing array
        if (ispostback)//if postback add only if the url is not the recent url and "mustadd" is true
        {
            if (alURLs.Count > 0)
                if (alURLs[alURLs.Count - 1].ToString().ToLower() == url.ToLower())
                    if (!mustAdd)
                        return;
        }
        if (alURLs.Count > 0)//check whether the last url is the url to be added, skip if yes
            if (alURLs[alURLs.Count - 1].ToString().ToLower() == url.ToLower())
                return;
        if (clearCache) alURLs.Clear();//if it is from the master grid, skip
        alURLs.Add(url);//add url
        HttpContext.Current.Session["URLs"] = alURLs;//load to session
    }
    public static string getURL(string curURL)//implemenetd by sanal on April 03, 2008
    {
        try
        {
            if (HttpContext.Current.Session["URLs"] == null) return "welcome.aspx?id=default";
            ArrayList alUrls = (ArrayList)HttpContext.Current.Session["URLs"];
            //for (int j = alUrls.Count; j < 0; j--)
            for (int j = 0; j < alUrls.Count; j++)//checks from the top to get a match for the current url
            {
                if (alUrls[j].ToString().ToLower() == curURL.ToLower())
                    return alUrls[j - 1].ToString();//retrieve the current url's master
            }
            return alUrls[alUrls.Count - 1].ToString();//return the last url, if no match, must be impossible scenario
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error " + e.Message + " in Navigator getURL Function");
            return "welcome.aspx?id=default";
        }
    }
}
